﻿jQuery(document).ready(function() {
    jQuery('#checkAllUser').live('click', function() {
        jQuery('#checkAllUser').showAllListUser();
    });

    jQuery('#checkAll').live('click', function() {
        jQuery('#checkAll').showAllList();
    });

    jQuery('#block-user-update li a').live('click', function() {
        var frm = document.forms.frmBlockUser;
        var value = jQuery(this).attr('rel');
        var valueID = [];
        jQuery('#btn-Update').val('Update');
        jQuery('#Event').val(value);

        jQuery('.checkListUser').each(function(index) {
            var checked = jQuery(this).attr('checked');
            if (checked == 'checked') {
                valueID.push(jQuery(this).val());
            }
        });

        if (value == 2) {
            if (valueID != '') {
                if (confirm('Are you sure you want to delete it?')) {
                    jQuery('#frmBlockUser').attr('action', '/admin/users/delete');
                    frm.submit();
                }
            } else {
                jQuery('#frmBlockUser').attr('action', '/admin/users/delete');
                frm.submit();
            }
        } else {
            jQuery('#frmBlockUser').attr('action', '/admin/users/checkStatus');
            frm.submit();
        }
    });

    /**
     * IA Attribute
     */
    jQuery('#block-ia-attribute li a').live('click', function() {
        var frm = document.forms.frmIaAttribute;
        var value = jQuery(this).attr('rel');
        var valueID = [];
        jQuery('#btn-Update').val('Update');
        jQuery('#Event').val(value);

        jQuery('.checkList').each(function(index) {
            var checked = jQuery(this).attr('checked');
            if (checked == 'checked') {
                valueID.push(jQuery(this).val());
            }
        });

        if (value == 2) {
            if (valueID != '') {
                if (confirm('Are you sure you want to delete it?')) {
                    jQuery('#frmIaAttribute').attr('action', '/admin/ia_attributes/delete');
                    frm.submit();
                }
            } else {
                jQuery('#frmIaAttribute').attr('action', '/admin/ia_attributes/delete');
                frm.submit();
            }
        } else {
            jQuery('#frmIaAttribute').attr('action', '/admin/ia_attributes/checkStatus');
            frm.submit();
        }
    });

    /**
     * Category List
     */
    jQuery('#block-manager-category li a').live('click', function() {
        var frm = document.forms.frmCategory;
        var value = jQuery(this).attr('rel');
        var valueID = [];
        jQuery('#btn-Update').val('Update');
        jQuery('#Event').val(value);

        jQuery('.checkList').each(function(index) {
            var checked = jQuery(this).attr('checked');
            if (checked == 'checked') {
                valueID.push(jQuery(this).val());
            }
        });

        if (value == 2) {
            if (valueID != '') {
                if (confirm('Are you sure you want to delete it?')) {
                    jQuery('#frmCategory').attr('action', '/admin/manager_category/delete');
                    frm.submit();
                }
            } else {
                jQuery('#frmCategory').attr('action', '/admin/manager_category/delete');
                frm.submit();
            }
        } else {
            jQuery('#frmCategory').attr('action', '/admin/manager_category/checkStatus');
            frm.submit();
        }
    });

    /**
     * Question List
     */
    jQuery('#block-question-list li a').live('click', function() {
        var frm = document.forms.frmQuestion;
        var value = jQuery(this).attr('rel');
        var valueID = [];
        jQuery('#btn-Update').val('Update');
        jQuery('#Event').val(value);

        jQuery('.checkList').each(function(index) {
            var checked = jQuery(this).attr('checked');
            if (checked == 'checked') {
                valueID.push(jQuery(this).val());
            }
        });

        if (value == 2) {
            if (valueID != '') {
                if (confirm('Are you sure you want to delete it?')) {
                    jQuery('#frmQuestion').attr('action', '/admin/manager_question_list/delete');
                    frm.submit();
                }
            } else {
                jQuery('#frmQuestion').attr('action', '/admin/manager_question_list/delete');
                frm.submit();
            }
        } else {
            jQuery('#frmQuestion').attr('action', '/admin/manager_question_list/checkStatus');
            frm.submit();
        }
    });

    /**
     * Question Tab List
     */
    jQuery('#block-manager-question-tab li a').live('click', function() {
        var frm = document.forms.frmQuestionTab;
        var value = jQuery(this).attr('rel');
        var valueID = [];
        jQuery('#btn-Update').val('Update');
        jQuery('#Event').val(value);

        jQuery('.checkList').each(function(index) {
            var checked = jQuery(this).attr('checked');
            if (checked == 'checked') {
                valueID.push(jQuery(this).val());
            }
        });

        if (value == 2) {
            if (valueID != '') {
                if (confirm('Are you sure you want to delete it?')) {
                    jQuery('#frmQuestionTab').attr('action', '/admin/manager_question_tab/delete');
                    frm.submit();
                }
            } else {
                jQuery('#frmQuestionTab').attr('action', '/admin/manager_question_tab/delete');
                frm.submit();
            }
        } else {
            jQuery('#frmQuestionTab').attr('action', '/admin/manager_question_tab/checkStatus');
            frm.submit();
        }
    });

    var parentCategorySelect = jQuery('#parentCategory');

    var loadChildCategory = function() {
        var id = parentCategorySelect.val();
        var childrenCategorySelect = jQuery('#childrenCategory');
        var activeValue = childrenCategorySelect.val();
        jQuery.ajax({
            url: "/admin/manager_question_tab/get_child_category",
            type: "get",
            data: {cid: id},
            dataType: 'json',
            success: function(res) {
                var html = '';
                if (res.length > 0) {
                    for (var i = 0; i < res.length; i++) {
                        html += '<option value="' + res[i].id + '">' + res[i].name + '</option>';
                    }
                    childrenCategorySelect.html(html);
                } else {
                    childrenCategorySelect.html('');
                };
                childrenCategorySelect.val(activeValue);
            }
        });
    }
    loadChildCategory();

    jQuery('#parentCategory').bind('change', function() {
        loadChildCategory();
    });

    jQuery( "#bilancoach_date" ).datepicker({ dateFormat: "dd/mm/yy" });

    jQuery( "#support_date" ).datepicker({dateFormat: "dd/mm/yy"});

    /* childrenCategory select controller */
    var childrenCategorySelect = jQuery('#childrenCategory');
    var childrenCategoryController = function(){
        var id = childrenCategorySelect.val();
        var questiontabSelect = jQuery('select:[name="questiontab_id"]');
        var activeValue = questiontabSelect.val();
        jQuery.ajax({
            url: "/admin/manager_question_list/get_question_tab",
            type: "get",
            data: {subcatid: id},
            dataType: 'json',
            success: function(res) {console.log(res);
                var html = '';
                if (res.length > 0) {
                    for (var i = 0; i < res.length; i++) {
                        html += '<option value="' + res[i].id + '">' + res[i].name + '</option>';
                    }
                    questiontabSelect.html(html);
                } else {
                    questiontabSelect.html('');
                };
                questiontabSelect.val(activeValue);
            }
        });
    }

    if(childrenCategorySelect.length !== 0){
        childrenCategorySelect.unbind('change.category').bind('change.category', function(){
            childrenCategoryController();
        });
    }
});